package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShoppingCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     *添加购物车
     */
    @Override
    public void addshoppingCar(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //
        List<ShoppingCart> shoppingCartList = shoppingCarMapper.list(shoppingCart);
        //先判断购物车里面是否已经有要插入的菜品或者套餐数据
        if (shoppingCartList != null && !shoppingCartList.isEmpty()){
            //如果有就将数量加一
            ShoppingCart Cart = shoppingCartList.get(0);
            Cart.setNumber(Cart.getNumber() + 1);
            shoppingCarMapper.addNumberById(Cart);
        } else {
            //如果没有就插入
            //判断本次插入的是菜品还是套餐
            Long DishId = shoppingCartDTO.getDishId();
            if(DishId != null){
                //本次插入的数据是菜品
                Dish dish = dishMapper.getById(DishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //本次插入的数据是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCarMapper.insertShoppingCar(shoppingCart);
        }
    }

    /**
     *查询购物车
     **/
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        return shoppingCarMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCarMapper.cleanByUserId(userId);
    }

    /**
     *删除购物车中一个商品
     */
    @Override
    public void deleteShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCarMapper.list(shoppingCart);
        ShoppingCart Cart = shoppingCartList.get(0);
        //判断本次删除之前的数量，如果是1就执行delete语句，如果大于一就只需要调用update修改数量即可
        if(Cart.getNumber() == 1){
            shoppingCarMapper.cleanByUserId(Cart.getUserId());
        }else if(Cart.getNumber() >= 1){
            Cart.setNumber(Cart.getNumber() - 1);
            Cart.setCreateTime(LocalDateTime.now());
            shoppingCarMapper.addNumberById(Cart);
        }
    }


}
